﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Wagner.SharePoint.Utilities
{
	internal static class Helper
	{
		internal static Guid GetJobID(string jobTitle, string url)
		{
			using (SPSite site = new SPSite(url))
			{
				SPWebApplication webApp = site.WebApplication;
				
				// Make sure the job exists.
				foreach (SPJobDefinition job in webApp.JobDefinitions)
				{
					if (job.Title.Equals(jobTitle, StringComparison.InvariantCultureIgnoreCase))
					{
						return job.Id;
					}
				}
				
				throw new Exception(string.Format("Job not found having title '{0}' for web application url {1}", jobTitle, url));
			}
		}

		internal static string GetWebAppUrl(SPWebApplication webApp)
		{
			foreach (SPAlternateUrl alt in webApp.AlternateUrls)
			{
				if (alt.UrlZone == SPUrlZone.Default)
					return alt.IncomingUrl;
			}

			using (SPSite site = webApp.Sites[0])
				return site.Url;
		}


	}
}
